package com.nageoffer.onecoupon.settlement;

import org.junit.jupiter.api.Test;

import java.util.*;

public class test {
    // TODO 利用多线程实现 1 a 2 b 3 c ….
    private static final Object lock = new Object();
    private static int count = 1;
    private static final int MAX_COUNT = 52;
    private static int num = 1;
    private static int charNum = 0;
    public static void main(String[] args) {
        Runnable printNum = () -> {
            while(count <= MAX_COUNT){
                synchronized (lock){
                    if(count % 2 != 0){
                        System.out.print(num++ + " ");
                        count++;
                        lock.notify();
                    }else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };

        Runnable printChar = () -> {
            while(count <= MAX_COUNT){
                synchronized (lock){
                    if(count % 2 == 0){
                        System.out.print((char)('a'+ charNum) + " ");
                        charNum++;
                        count++;
                        lock.notify();
                    }else {
                        try {
                            lock.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        };
        new Thread(printNum).start();
        new Thread(printChar).start();
    }

    // TODO 辗转反侧法实现最大公约数
    @Test
    public void test3(){
        Scanner scanner = new Scanner(System.in);
        int numa = scanner.nextInt();
        int numb = scanner.nextInt();
        // int numa = 115;    // 72
        // int numb = 15;    // 18

        if (numa < numb){
            int temp = numa;
            numa = numb;
            numb = temp;
        }

        while (numa % numb != 0){   // 115 % 15 = 10 // 15 % 10 = 5 // 10 % 5 = 0
            int temp = numa % numb;
            numa = numb;
            numb = temp;
            System.out.println("temp = " + temp + ",numa = " + numa + ",numb = " + numb);
        }

        System.out.println(numb);
    }

    // TODO 利用自己的方法实现堆栈
    @Test
    public void test4(){
        MyStack<Integer> stack = new MyStack<>();
        stack.push(1);
        stack.push(2);
        stack.push(3);

        System.out.println("Size: " + stack.size());  // 3
        System.out.println("Peek: " + stack.peek()); // 3

        while (!stack.isEmpty()) {
            System.out.println("Pop: " + stack.pop());
        }
        // 输出顺序：3, 2, 1
    }
}

class MyStack<E>{
    private static class Node<E>{
        E data;
        Node<E> next;

        Node(E data){
            this.data = data;
        }
    }

    private Node<E> top;
    private int size;

    public MyStack(){
        top = null;
        size = 0;
    }

    // push
    public void push(E data){
        Node<E> node = new Node<E>(data);
        node.next = top;    // 新节点指向栈顶
        top = node;    // 更新栈顶指针
        size++;
    }

    public E pop(){
        if (isEmpty()){
            return null;
        }
        E data = top.data;
        top = top.next;
        size--;
        return data;
    }

    // 查看栈顶元素（O(1)时间复杂度）
    public E peek() {
        if (isEmpty()) {
            throw new EmptyStackException();
        }
        return top.data;
    }

    // 判断空栈（O(1)）
    public boolean isEmpty() {
        return size == 0;
    }

    // 获取元素数量（O(1)）
    public int size() {
        return size;
    }
}